/* @获取书籍搜索组件的历史搜索信息
* @Author: luocheng
* @Date:   2020-01-06 22:45:24
* @Last Modified by:   luocheng
* @Last Modified time: 2020-01-07 20:46:40
*/
import {
	HTTP
} from '../util/http-p.js'

class KeywordModel extends HTTP {
	key = 'q'
	maxLenght = 10
	// 获取历史搜索标签
	getHistory(){
		const words = wx.getStorageSync(this.key)
		if (!words) {
			return []
		}
		return words
	}

	// 获取热门搜索标签
	getHot(){
		return this.request({
			url:'/book/hot_keyword'
		})
	}

	// 将搜索过的词写入缓存
	addToHistory(keyword){
		let words = this.getHistory()
		const has = words.includes(keyword)
		if (!has) {
			const length = words.length
			if (length >= this.maxLenght) {
				words.pop()
			}
			words.unshift(keyword)
			wx.setStorageSync(this.key, words)
		}
		
	}
}

export {KeywordModel}